6.2.2 String Concatenation

It seemed like a good idea at the time.

Brian Kernighan

There is only one string operation: concatenation. It does not have a specific operator to represent it. Instead, concatenation is performed by writing expressions next to one another, with no operator. For example:

$ awk '{ print "Field number one: " $1 }' mail-list
-| Field number one: Amelia
-| Field number one: Anthony
...

Without the space in the string constant after the ‘:’, the line runs together. For example:

$ awk '{ print "Field number one:" $1 }' mail-list
-| Field number one:Amelia
-| Field number one:Anthony
...

Because string concatenation does not have an explicit operator, it is often necessary to ensure that it happens at the right time by using parentheses to enclose the items to concatenate. For example, you might expect that the following code fragment concatenates file and name:

file = "file"
name = "name"
print "something meaningful" > file name

This produces a syntax error with some versions of Unix awk.35 It is necessary to use the following:

print "something meaningful" > (file name)

Parentheses should be used around concatenation in all but the most common contexts, such as on the righthand side of ‘=’. Be careful about the kinds of expressions used in string concatenation. In particular, the order of evaluation of expressions used for concatenation is undefined in the awk language. Consider this example:

BEGIN {
    a = "don't"
    print (a " " (a = "panic"))
}

It is not defined whether the second assignment to a happens before or after the value of a is retrieved for producing the concatenated value. The result could be either ‘don't panic’, or ‘panic panic’.

The precedence of concatenation, when mixed with other operators, is often counter-intuitive. Consider this example:

$ awk 'BEGIN { print -12 " " -24 }'
-| -12-24

This “obviously” is concatenating −12, a space, and −24. But where did the space disappear to? The answer lies in the combination of operator precedences and awk’s automatic conversion rules. To get the desired result, write the program this way:

$ awk 'BEGIN { print -12 " " (-24) }'
-| -12 -24

This forces awk to treat the ‘-’ on the ‘-24’ as unary. Otherwise, it’s parsed as follows:

    −12 (" " − 24)
⇒ −12 (0 − 24)
⇒ −12 (−24)
⇒ −12−24

As mentioned earlier, when mixing concatenation with other operators, parenthesize. Otherwise, you’re never quite sure what you’ll get.


Footnotes

(35)

It happens that BWK awk, gawk, and mawk all “get it right,” but you should not rely on this.